<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./style/index.css" />
</head>

<body>
    <div id="app">
        <header class="header">
            <h1>记事本</h1>
            <input placeholder="请输入任务" class="new-todo" v-model="todoName" />
            <button @click="add" class="add">添加任务</button>
        </header>
        <section class="main">
            <ul class="todo-list">
                <li class="todo" v-for="(item, index) in list" :key="item.id">
                    <div class="view">
                        <span class="index">{{index + 1}}.</span>
                        <label>{{item.name}}</label>
                        <button @click="del(item.id)" class="destroy"></button>
                    </div>
                </li>
            </ul>
        </section>
        
        <footer class="footer" v-show="list.length > 0">
            <span class="todo-count">合 计:<strong>{{list.length}}</strong></span>
            <button @click="clearall" class="clear-completed">
                清空任务
            </button>
        </footer>

    </div>

    <script src="../lib/vue.js"></script>

    <script>
        const app = new Vue({
            el: "#app",
            data() {
                return {
                    list: [
                        { id: 1, name: "跑步1公里" },
                        { id: 2, name: "跳绳200次" },
                        { id: 3, name: "举重200次" },
                    ],
                    todoName: ""
                }
            },
            methods: {
                del(id) {
                    this.list = this.list.filter(item => item.id !== id)
                },
                add() {
                    if (this.todoName.trim() == "") {
                        alert("请输入名称")
                        return
                    } 
                    this.list.unshift(
                        { id: new Date(), name: this.todoName },
                    )
                    this.todoName = ""
                },
                clearall() {
                    this.list = []
                }
            }

        })
    </script>
</body>

</html>